package org.shuma.goods.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.shuma.goods.domain.DTO.QueryOperatorRecord;
import org.shuma.goods.domain.entity.OperationRecord;
import org.shuma.goods.mapper.OperationRecordMapper;
import org.shuma.goods.service.OperationRecordService;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author taoha
* @description 针对表【operation_record】的数据库操作Service实现
* @createDate 2024-06-17 19:42:56
*/
@Service
public class OperationRecordServiceImpl extends ServiceImpl<OperationRecordMapper, OperationRecord>
    implements OperationRecordService {

    /**
     * 查询操作记录
     * @param queryOperatorRecord
     * @param userId
     * @return
     */
    @Override
    public Page<OperationRecord> listPageRecord(QueryOperatorRecord queryOperatorRecord, Long userId) {


        Page<OperationRecord> page  = new Page<>(queryOperatorRecord.getPageNum(), queryOperatorRecord.getPageSize());
        LambdaQueryWrapper<OperationRecord> queryWrapper  = new LambdaQueryWrapper<>();
        queryWrapper.eq(OperationRecord::getOperatorId, userId);

        Page<OperationRecord> resultPage = this.page(page,queryWrapper);


        // 定义解析输入日期字符串的 SimpleDateFormat
        SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");

        // 定义目标格式的 DateTimeFormatter
        DateTimeFormatter targetFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 从 resultPage 中获取记录，并转换时间格式
        List<OperationRecord> records = resultPage.getRecords();
        List<OperationRecord> formattedRecords = records.stream().map(record -> {
            try {
                // 将 Date 对象转换为输入格式的字符串
                String dateString = inputFormat.format(record.getOperationTime());

                // 解析字符串为 Date 对象
                Date parsedDate = inputFormat.parse(dateString);

                // 将 Date 对象转换为 LocalDateTime
                LocalDateTime dateTime = parsedDate.toInstant()
                        .atZone(ZoneId.systemDefault())
                        .toLocalDateTime();

                // 将 LocalDateTime 格式化为目标格式
                String formattedDate = dateTime.format(targetFormatter);

                // 设置格式化后的日期回到 record 中
                record.setOperationTime(java.sql.Date.valueOf(formattedDate));
            } catch (ParseException e) {
                e.printStackTrace(); // 处理解析异常
            }
            return record;
        }).collect(Collectors.toList());



        // 更新 resultPage 的记录
        resultPage.setRecords(formattedRecords);

        return resultPage;
    }
}




